Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPENNLP-1607: SimpleClassPathModelFinder not returning list of matching paths #652

Merged

Conversation

codyfearer
Copy link
Contributor

@codyfearer codyfearer commented Sep 14, 2024

Updated SimpleClassPathModelFinder to return a list of paths instead of the delimiter

After

Thank you for contributing to Apache OpenNLP.

In order to streamline the review of the contribution we ask you
to ensure the following steps have been taken:

For all changes:

  • Is there a JIRA ticket associated with this PR? Is it referenced
    in the commit message?

  • Does your PR title start with OPENNLP-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.

  • Has your PR been rebased against the latest commit within the target branch (typically main)?

  • Is your initial contribution a single, squashed commit?

For code changes:

  • Have you ensured that the full suite of tests is executed via mvn clean install at the root opennlp folder?
  • Have you written or updated unit tests to verify your changes?
  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?
  • If applicable, have you updated the LICENSE file, including the main LICENSE file in opennlp folder?
  • If applicable, have you updated the NOTICE file, including the main NOTICE file found in opennlp folder?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered?

Note:

Please ensure that once the PR is submitted, you check GitHub Actions for build issues and submit an update to your PR as soon as possible.

Updated SimpleClassPathModelFinder to return a list of paths instead of the delimiter
@mawiesne
Copy link
Contributor

@codyfearer Thx for the PR. A screenshot is not a proof of correctness for the intended behavior. Could you therefore add a test case that demonstrates the difference in outcomes with your code change?

@rzo1
Copy link
Contributor

rzo1 commented Sep 16, 2024

There is already a unit test but the branch isn't hit because of the surefire configuration.

@codyfearer Maybe just add an additional execution phase for surefire without the --add-opens, so the branch actually gets triggered. This will show, that the current impl is broken and your patch is a fix for it.

Something like

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.plugin}</version>
                <configuration>
                     <forkCount>${opennlp.forkCount}</forkCount>
                    <useSystemClassLoader>false</useSystemClassLoader>
                    <failIfNoSpecifiedTests>false</failIfNoSpecifiedTests>
                </configuration>
                <executions>
                    <execution>
                        <id>ucp</id>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <argLine>-Xmx2048m -Dorg.slf4j.simpleLogger.defaultLogLevel=off --add-opens java.base/jdk.internal.loader=ALL-UNNAMED</argLine>
                        </configuration>
                    </execution>
 <!-- This phase is here to trigger a fallback to the classpath from the system properties, which cannot be hit, if --add-opens is present. -->
                    <execution>
                        <id>no-reflection</id>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <argLine>-Xmx2048m -Dorg.slf4j.simpleLogger.defaultLogLevel=off</argLine>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

There might be better names for the id section.

Copy link
Contributor

@rzo1 rzo1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check CI results? Looks like this change is broken on Windows? :)

I guess, that the additional surefire phase now reveals another bug (with Windows). I think, that it needs an adjustment of the path similar to the adjustment done in private List<URI> getURIsFromJar(URL fileUrl, boolean isWindows) throws IOException

@rzo1
Copy link
Contributor

rzo1 commented Sep 18, 2024

@codyfearer I think

Index: opennlp-tools-models/src/main/java/opennlp/tools/models/simple/SimpleClassPathModelFinder.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/opennlp-tools-models/src/main/java/opennlp/tools/models/simple/SimpleClassPathModelFinder.java b/opennlp-tools-models/src/main/java/opennlp/tools/models/simple/SimpleClassPathModelFinder.java
--- a/opennlp-tools-models/src/main/java/opennlp/tools/models/simple/SimpleClassPathModelFinder.java	(revision 8d3b2dacc1da8d301734e9004b998b001f6619b0)
+++ b/opennlp-tools-models/src/main/java/opennlp/tools/models/simple/SimpleClassPathModelFinder.java	(date 1726648019585)
@@ -63,8 +63,8 @@
 
   private static final Logger logger = LoggerFactory.getLogger(SimpleClassPathModelFinder.class);
   private static final String FILE_PREFIX = "file";
-  private static final Pattern CLASSPATH_SEPARATOR_PATTERN = Pattern.compile("[;:]");
-  // ; for Windows, : for Linux/OSX
+  private static final Pattern CLASSPATH_SEPARATOR_PATTERN_WINDOWS = Pattern.compile(";");
+  private static final Pattern CLASSPATH_SEPARATOR_PATTERN_UNIX = Pattern.compile(":");
 
   /**
    * By default, it scans for "opennlp-models-*.jar".
@@ -190,20 +190,26 @@
       if (fromUcp != null && fromUcp.length > 0) {
         return Arrays.asList(fromUcp);
       } else {
-        final String cp = System.getProperty("java.class.path", "");
-        final String[] matches = CLASSPATH_SEPARATOR_PATTERN.split(cp);
-        final List<URL> jarUrls = new ArrayList<>();
-        for (String classPath: matches) {
-          try {
-            jarUrls.add(new URL(FILE_PREFIX, "", classPath));
-          } catch (MalformedURLException ignored) {
-            //if we cannot parse a URL from the system property, just ignore it...
-            //we couldn't load it anyway
-          }
-        }
-        return jarUrls;
-      }
-    }
+        return getClassPathUrlsFromSystemProperty();
+      }
+    }
+  }
+
+  private List<URL> getClassPathUrlsFromSystemProperty() {
+    final String cp = System.getProperty("java.class.path", "");
+    final String[] matches = isWindows()
+            ? CLASSPATH_SEPARATOR_PATTERN_WINDOWS.split(cp)
+            : CLASSPATH_SEPARATOR_PATTERN_UNIX.split(cp);
+    final List<URL> jarUrls = new ArrayList<>();
+    for (String classPath: matches) {
+      try {
+        jarUrls.add(new URL(FILE_PREFIX, "", classPath));
+      } catch (MalformedURLException ignored) {
+        //if we cannot parse a URL from the system property, just ignore it...
+        //we couldn't load it anyway
+      }
+    }
+    return jarUrls;
   }
 
   /*

should do the trick on Windows. The issue with the combined regex is, that Windows cp elements start with "C:" for example and a split with "[;:]" will be broken in that case.

Updating getClassPathUrlsFromSystemProperty to handle Windows paths
@codyfearer
Copy link
Contributor Author

Added the code for Windows paths. Was able to successfully build on a Windows 11 VM

@jzonthemtn jzonthemtn merged commit 025f1b7 into apache:main Sep 20, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants